Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 13/30 Β· 107.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

| Example | Returned value rounded to 5 digits |
|---|---|
| Math.abs(-2.3) | 2.3 |
| Math.acos(Math.SQRT1_2) | 0.78540 rad = 45Β° |
| Math.asin(Math.SQRT1_2) | 0.78540 rad = 45Β° |
| Math.atan(1) | 0.78540 rad = 45Β° |
| Math.atan2(-3.7, -3.7) | βˆ’2.3562 rad = βˆ’135Β° |
| Math.ceil(1.1) | 2 |
| Math.cos(Math.PI/4) | 0.70711 |
| Math.exp(1) | 2.7183 |
| Math.floor(1.9) | 1 |
| Math.log(Math.E) | 1 |
| Math.max(1, -2) | 1 |
| Math.min(1, -2) | βˆ’2 |
| Math.pow(-3, 2) | 9 |
| Math.random() | e.g. 0.17068 |
| Math.round(1.5) | 2 |
| Math.sin(Math.PI/4) | 0.70711 |
| Math.sqrt(49) | 7 |
| Math.tan(Math.PI/4) | 1 |

| Example | Description |
|---|---|
| Math.abs(-2.3) | Absolute value |
| Math.acos(Math.SQRT1_2) | Arccosine |
| Math.asin(Math.SQRT1_2) | Arcsine |
| Math.atan(1) | Half circle arctangent ( ⁠ βˆ’ Ο€ / 2 {\d… |
| Math.atan2(-3.7, -3.7) | Whole circle arctangent ( ⁠ βˆ’ Ο€ {\disp… |
| Math.ceil(1.1) | Ceiling: round up to smallest integer β‰₯… |
| Math.cos(Math.PI/4) | Cosine |
| Math.exp(1) | Exponential function : e raised to this… |
| Math.floor(1.9) | Floor: round down to largest integer ≀… |
| Math.log(Math.E) | Natural logarithm, base e |
| Math.max(1, -2) | Maximum: (x > y) ? x : y |
| Math.min(1, -2) | Minimum: (x < y) ? x : y |
| Math.pow(-3, 2) | Exponentiation (raised to the power of)… |
| Math.random() | Pseudorandom number between 0 (inclusiv… |
| Math.round(1.5) | Round to the nearest integer; half frac… |
| Math.sin(Math.PI/4) | Sine |
| Math.sqrt(49) | Square root |
| Math.tan(Math.PI/4) | Tangent |

Regular expression

/expression/.test(string); // returns Boolean
"string".search(/expression/); // returns position Number
"string".replace(/expression/, replacement);
// Here are some examples
if (/Tom/.test("My name is Tom")) console.log("Hello Tom!");
console.log("My name is Tom".search(/Tom/)); // == 11 (letters before
Tom)
console.log("My name is Tom".replace(/Tom/, "John")); // == "My name is
John"

Character classes

// \d - digit
// \D - non digit
// \s - space
// \S - non space
// \w - word char
// \W - non word
// [ ] - one of
// [^] - one not of
// - - range
if (/\d/.test('0')) console.log('Digit');
if (/[0-9]/.test('6')) console.log('Digit');
if (/[13579]/.test('1')) console.log('Odd number');
if (/\S\S\s\S\S\S\S/.test('My name')) console.log('Format OK');
if (/\w\w\w/.test('Tom')) console.log('Hello Tom');
if (/[a-zA-Z]/.test('B')) console.log('Letter');

Character matching

// A...Z a...z 0...9 - alphanumeric
// \u0000...\uFFFF - Unicode hexadecimal
// \x00...\xFF - ASCII hexadecimal
// \t - tab
// \n - new line
// \r - CR
// . - any character
// | - OR
if (/T.m/.test('Tom')) console.log ('Hi Tom, Tam or Tim');
if (/A|B/.test("A")) console.log ('A or B');

Repeaters

//Β ? - 0 or 1 match
// * - 0 or more
// + - 1 or more
// {n} - exactly n
// {n,} - n or more
// {0,n} - n or less
// {n,m} - range n to m
if (/ab?c/.test("ac")) console.log("OK"); // match: "ac", "abc"
if (/ab*c/.test("ac")) console.log("OK"); // match: "ac", "abc", "abbc",
"abbbc" etc.
if (/ab+c/.test("abc")) console.log("OK"); // match: "abc", "abbc",
"abbbc" etc.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────